<template>
  <div class="container">
    <el-card>
      <el-row>
        <el-col :span="2">
          <h4 class="fonts">学科名称</h4>
        </el-col>
        <el-col :span="5">
          <el-input v-model="pageInfo.subjectName"></el-input>
        </el-col>
        <el-col :span="2" class="searchinput">
          <el-button @click="deletesubName">清除</el-button>
        </el-col>
        <el-col :span="2" class="op">
          <el-button type="primary" @click="getsubjectbyname">搜索</el-button>
        </el-col>
        <el-col :span="1" class="addsub">
          <el-button type="success" size="small" icon="el-icon-edit" @click="addSubjectDia"
            >新增学科</el-button
          >
        </el-col>
      </el-row>
      <el-alert
        :title="'数据一共' + total + '条'"
        type="info"
        show-icon
        class="info"
        :closable="false"
      >
      </el-alert>
      <!-- 表格区域 -->
      <el-table :data="subdata" stripe>
        <el-table-column type="index" prop="index" label="序号">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="200px">
          <template slot-scope="scope">
            {{ scope.row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template slot-scope="scope">
            <span v-if="scope.row.isFrontDisplay === 1">是</span>
            <span v-else>否 </span>
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column
          label="操作"
          width="300px
        "
          ><template slot-scope="scope">
            <el-button type="text" @click="junpsubjectclass(scope.row)"
              >学科分类</el-button
            >
            <el-button type="text" @click="jumpsubtags(scope.row)"
              >学科标签</el-button
            >
            <el-button type="text" @click="amend(scope.row)">修改</el-button>
            <el-button type="text" @click="open(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 这是修改的弹窗 -->
      <el-dialog
        title="修改学科"
        :visible.sync="dialogVisible"
        width="30%"
        @close="resetEditor"
      >
        <!-- :before-close="handleCloseeditor" -->

        <!-- 修改学科的表单 -->
        <el-form
          :model="upData"
          :rules="rules"
          ref="editorFormref"
          label-width="100px"
        >
          <el-form-item label="学科名称" prop="subjectNameamend">
            <template>
              <el-input v-model="upData.subjectNameamend"></el-input>
            </template>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              v-model="switchon"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch></el-form-item
        ></el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="subupdata">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 新增学科 -->
      <el-dialog
        title="新增学科"
        :visible.sync="adddialogVisible"
        width="30%"
        @close="closeAddSub"
      >
        <el-form
          :model="addSubData"
          :rules="rules"
          ref="addsubjectref"
          label-width="100px"
        >
          <el-form-item label="学科名称" prop="addsubName">
            <template>
              <el-input v-model="addSubData.addsubName"></el-input>
            </template>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              v-model="addSubData.switchOnOff"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch></el-form-item
        ></el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="adddialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="comfirmAddSub">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 分页按钮 -->
      <el-pagination
        @size-change="SizeChange"
        @current-change="CurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[5, 10]"
        :page-size="pageInfo.pagesize"
        layout=" sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { list, update, remove, add } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      // subJect: '', // 搜索框的学科名称
      total: null,
      subdata: [], // 获取到的学科列表
      // 分页的数据
      pageInfo: {
        subjectName: '',
        page: 1,
        pagesize: 5
      },
      dialogVisible: false, // 控制弹窗的显示或者隐藏
      deldialogVisible: false, // 删除按钮的弹窗
      adddialogVisible: false, // 新增学科
      rules: {
        addsubName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ],
        subjectNameamend: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      // 修改时展示的名称
      upData: {
        subjectNameamendId: '',
        subjectNameamend: '',
        // 是否显示
        isFrontDisplay: '' // 这是个布尔值
      },
      switchon: true,
      addSubData: {
        addsubName: '',
        switchOnOff: 0
      }
    }
  },
  created () {
    this.getsubject()
  },
  methods: {
    // 获取学科详情
    async getsubject () {
      const { data } = await list(this.pageInfo)
      this.total = data.counts
      this.subdata = data.items
    },
    // 每页显示多少条发生改变的时触发
    SizeChange (val) {
      this.pageInfo.pagesize = val
      this.getsubject()
    },
    // 当前页发生改变时候
    CurrentChange (val) {
      this.pageInfo.page = val
      this.getsubject()
    },
    // 输入学科名称查询学科列表
    getsubjectbyname () {
      this.getsubject()
    },
    // 点击清除按钮
    deletesubName () {
      this.pageInfo.subjectName = ''
      this.getsubject()
    },
    // 跳转学科分类页面
    junpsubjectclass (obj) {
      this.$router.push({
        path: '/subjects/directorys',
        query: {
          id: obj.id,
          name: obj.subjectName
        }
      })
    },
    // 跳转学科标签页面
    jumpsubtags (obj) {
      this.$router.push({
        path: '/subjects/tags',
        query: {
          id: obj.id,
          name: obj.subjectName
        }
      })
    },
    // 点击修改按钮
    async amend (val) {
      this.upData.subjectNameamendId = val.id
      this.upData.subjectNameamend = val.subjectName
      this.upData.isFrontDisplay = val.isFrontDisplay
      this.dialogVisible = true
      if (this.upData.isFrontDisplay === 1) {
        this.switchon = true
      } else {
        this.switchon = false
      }
    },
    subupdata () {
      this.$refs.editorFormref.validate(async valid => {
        if (valid) {
          if (this.switchon) {
            this.upData.isFrontDisplay = 1
          } else {
            this.upData.isFrontDisplay = 0
          }
          // 发起修改请求
          await update({
            id: this.upData.subjectNameamendId,
            subjectName: this.upData.subjectNameamend,
            isFrontDisplay: this.upData.isFrontDisplay
          })
          this.getsubject()
          this.dialogVisible = false
        }
      })
    },
    // 确认删除的messageBox
    open (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        // 删除操作后续
        .then(async () => {
          // 发起删除请求
          console.log(id)
          try {
            await remove({ id: id })
            this.$message.success('删除成功!')
          } catch (err) {
            this.$message('删除失败')
          }
          this.getsubject()
        })
        // 取消删除操作
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 增加学科弹窗
    addSubjectDia () {
      this.adddialogVisible = true
    },
    // 点击添加学科确认按钮后续
    async comfirmAddSub () {
      // 表单预校验
      this.$refs.addsubjectref.validate(async valid => {
        if (valid) {
          let isFrontDisplay = 0
          if (this.addSubData.switchOnOff) {
            isFrontDisplay = 1
          } else {
            isFrontDisplay = 0
          }
          await add({
            subjectName: this.addSubData.addsubName,
            isFrontDisplay: isFrontDisplay
          })
          this.getsubject()
          this.adddialogVisible = false
        }
      })
    },
    // 关闭新增学科对话框
    closeAddSub () {
      this.$refs.addsubjectref.resetFields()
    },
    // 关闭新增学科修改
    resetEditor () {
      this.$refs.editorFormref.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
.fonts {
  margin: 0px;
  padding: 0px;
  position: relative;
  top: 10px;
  left: 20px;
}
.searchinput {
  margin-left: 10px;
}
.info {
  margin: 20px 0px;
}
.op {
  position: relative;
  right: 32px;
}
.addsub {
  float: right;
  margin-right: 100px;
}
</style>
